package main

import (
	"chat/server/models"
	"fmt"
	"io"
	"net"
	"os"
	"time"
)

func initUserDao() {
	models.MyUserDao = models.NewUserDao(pool)
}

func process(conn net.Conn) {
	defer conn.Close()
	processor := Processor{
		Conn: conn,
	}
	err := processor.process2()
	if err != nil {
		if err == io.EOF {
			fmt.Println("客户端退出连接...")
		} else {
			fmt.Println("通信协程错误，err=", err)
		}
		return
	}
}
func close() {
	for {
		var key int
		fmt.Scanf("%v\n", &key)
		if key == 77 {
			os.Exit(0)
		}
	}
}
func main() {
	InitPool(8, 0, 300*time.Second, "localhost:6379")
	initUserDao()
	listen, err := net.Listen("tcp", "localhost:7788")
	if err != nil {
		fmt.Println("连接错误，err=", err)
	}
	defer listen.Close()
	go close()

	for {
		fmt.Println("waitting...")
		conn, err := listen.Accept()
		if err != nil {
			fmt.Println("连接错误，err=", err)
		}
		go process(conn)
	}
}
